package com.ctn.information.util;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMInfoAgent;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.DirectShareListener;
import com.umeng.socialize.controller.listener.SocializeListeners.UMAuthListener;
import com.umeng.socialize.exception.SocializeException;

/**
 * 社会化分享组件
 * @author loke.wei
 *
 */
public class SocialUtil {
	
	public static Map<String,UMSocialService> socialCtrls = new HashMap<String, UMSocialService>();
	
	public static final String DESCRIPTOR = "dzm";

	/**
	 * 打开带有列表的分享界面
	 * @param context android上下文
	 * @param shareContent 分享的内容
	 * @param des	分享控制器的id
	 */
	public static void openShareView(Context context,String shareContent,String des){
		UMSocialService controller = getController(des);
		//设置默认分享文字
		controller.setShareContent(shareContent);
		controller.openShare(context,false);
	}
	
	private static UMSocialService getController(String des){
		UMSocialService controller = socialCtrls.get(des);
		if(null==controller){
			controller = UMServiceFactory.getUMSocialService(des, RequestType.SOCIAL);
			socialCtrls.put(des, controller);
		}
		return controller;
	}
	
	/**
	 * 直接后台分享内容，不打开分享列表
	 * @param context android上下文
	 * @param platform 分享到哪个平台(SHARE_MEDIA[SINA, TENCENT, RENREN, DOUBAN, QZONE])
	 * @param shareContent 分享的内容
	 * @param des 分享控制器的id
	 */
	public static void directShare(Context context,SHARE_MEDIA platform,String shareContent,String des){
		UMSocialService controller = getController(des);
		final Context mContext = context;
		//设置默认分享文字
		controller.setShareContent("分享内容");

		DirectShareListener directShareListener = new DirectShareListener() {
		    @Override
		    public void onOauthComplete(String usid, SHARE_MEDIA platform) {
		        if (!TextUtils.isEmpty(usid)) {
		            //授权成功（可打开分享页）
		            Toast.makeText(mContext, "授权成功【usid:" + usid + "】",
		                    Toast.LENGTH_SHORT).show();
		        } else {
		            //授权失败（不可打开分享页）
		            Toast.makeText(mContext, "授权失败,请重试！", Toast.LENGTH_LONG).show();
		        }
		    }

		    @Override
		    public void onAuthenticated(SHARE_MEDIA platform) {
		        //已经授权，直接打开分享页
		        Toast.makeText(mContext, "已授权，直接打开。", Toast.LENGTH_SHORT).show();
		    }
		};
		controller.directShare(mContext, platform, directShareListener);
	}
	
	public static UMSocialService getShareView(String des){
		return socialCtrls.get(des);
	}
	
	/**
	 * 打开用户中心
	 * @param context
	 * @param descript
	 */
	public static void openSocialCenter(Context context,String des){
		getController(des).openUserCenter(context);
	}
	
	/**
	 * 执行对指定平台的授权登陆
	 * @param context
	 * @param platform
	 * @param des
	 */
	public static void openOauthVerify(final Context context,SHARE_MEDIA platform,String des){
		UMSocialService controller = getController(des);
		if (UMInfoAgent.isOauthed(context, platform)) {
			Toast.makeText(context, "该平台已经授权.", Toast.LENGTH_LONG).show();
		} else {
			controller.doOauthVerify(context, platform, new UMAuthListener() {
				@Override
				public void onError(SocializeException e, SHARE_MEDIA platform) {
				}

				@Override
				public void onComplete(Bundle value, SHARE_MEDIA platform) {
					if (value != null && !TextUtils.isEmpty(value.getString("uid"))) {
						Toast.makeText(context, "授权成功.", Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(context, "授权失败", Toast.LENGTH_SHORT).show();
					}
				}

				@Override
				public void onCancel(SHARE_MEDIA arg0) {
					Toast.makeText(context, "取消授权", Toast.LENGTH_SHORT).show();
				}

				@Override
				public void onStart(SHARE_MEDIA arg0) {
				}

			});
		}
	
	}
	
}
